﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 字符串至少匹配指定的正则表达式之一, 示例:
/// <code>
/// [VMustMatchRegexAtleastOne(["^1[0-9]{10}$", "^\\+86\\s{1,4}[0-9]{10}$"], "手机号码格式不正确, 正确的示例: 【13201241232】或【+86 13201241232】")]
/// public string PhoneNumber { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustMatchRegexAtleastOneAttribute(string[] patterns, string errorMessage = null) : Attribute, IValidateAction
{
    public string[] Patterns { get; set; } = patterns;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustMatchRegexAtleastOneInternal(ctx, ctx.ModelValue?.ToString(), Patterns, ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则字符串至少匹配指定的正则表达式之一, 示例:
/// <code>
/// [VMustMatchRegexAtleastOneIfNotNull(["^1[0-9]{10}$", "^\\+86\\s{1,4}[0-9]{10}$"], "手机号码格式不正确, 正确的示例: 【13201241232】或【+86 13201241232】")]
/// public string PhoneNumber { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustMatchRegexAtleastOneIfNotNullAttribute(string[] patterns, string errorMessage = null) : Attribute, IValidateAction
{
    public string[] Patterns { get; set; } = patterns;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateStringExtensions.MustMatchRegexAtleastOneInternal(ctx, ctx.ModelValue?.ToString(), Patterns, ErrorMessage);
    }
}
